var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tel: '',
    passw: '',
    url: getApp().globalData.url,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      currentTab: 0 //当前页的一些初始数据，视业务需求而定
    })
    this.onLoad(); //重新加载onLoad()
  },

  /**
   * 获取用户进入过最多的页面
   */
  getPageId: function() {
    var that = this
    wx.request({
      url: this.data.url + 'getMostEnterPage',
      method: 'GET',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        doctorTel: getApp().globalData.username,
      },
      success: function (res) {
        if (res.data == 9) {
          wx.switchTab({
            url: '../doctors/doctors',
          })
        }
        else if(res.data == 10){
          wx.switchTab({
            url: '../consultList/consultList',
          })
        }
        else if(res.data == 11){
          wx.switchTab({
            url: '../mine/mine',
          })
        }
      }
    })
  },

  /**
   * 用户点击登录
   */
  loginBtnClick: function(e) {
    var that=this;

    if (this.data.tel == "" || this.data.tel == undefined) 
    {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return;
    } 
    else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.data.tel))) 
    {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none'
      })
      return;
    }

    if (this.data.passw == '' || this.data.passw == undefined) 
    {
      wx.showToast({
        title: '请输入密码',
        icon: 'none',
        duration: 2000
      })
      return;
    } 
    else 
    {
      var that = this
      wx.request({
        url: this.data.url + 'doctorlogin',
        method: 'GET',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        data: {
          doctorTel: this.data.tel,
          doctorPassword: this.data.passw
        },
        success: function(res) 
        {
          if (res.data == "False" || res.data == false) 
          {
            wx.showToast({
              title: '用户名或密码错误',
              icon: "none",
              duration: 2000
            })
          } 
          else if (res.data == "Doctor") 
          {
            wx.showToast({
              title: '此账号为医生账号，请前往医生小程序进行登录',
              icon: 'none'
            })
          }
          else if (res.data == "Assistent")
          {
            app.globalData.username = that.data.tel,
            that.getPageId()
          }
          else {
            wx.showToast({
              title: '登录失败，请重新尝试',
              icon: 'none'
            })
          }
        },
        fail(res) {
          wx.showToast({
            title: '网络连接失败，请重新尝试',
            icon: 'none'
          })
        }
      })
    }
  },

  usernameInput: function(e) {
    this.setData({
      tel: e.detail.value
    })
  },

  passwordInput : function(e) {
    this.setData({
      passw: e.detail.value
    })
  },

  resetPasswords : function(e) {
    wx.navigateTo({
      url: '../resetpasswords/resetpasswords',
    })
  }
})